//
//  CeoZhuShouXVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/17.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "CeoZhuShouXVC.h"

#import "RKTagsMenuView.h"
#import "TodoHttpManager.h"
#import "CeoZhuDingVC.h"
#import "CeoZhuXCell.h"
#import "LXAlertView.h"
@interface CeoZhuShouXVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *_unitStr;
}

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *oneArr;
@property(nonatomic,strong)NSMutableArray *nameArr;
@property(nonatomic,strong)NSMutableDictionary *zhuUnitDic;

@property(nonatomic,strong)NSMutableArray *hosZArr;
@property(nonatomic,strong)NSMutableDictionary *hosMDic;
@property(nonatomic,strong)NSMutableArray *floorArr;
@property(nonatomic,strong)NSMutableArray *hangArr;
@property(nonatomic,strong)NSMutableArray *wenArr;
@property(nonatomic,strong)NSMutableArray *erDateArr;
@property(nonatomic,strong)NSMutableArray *room_numArr;

@property(nonatomic,copy)NSString *UserID;
@end

@implementation CeoZhuShouXVC

-(void)viewWillAppear:(BOOL)animated
{
    
    //主售房源单元
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoMeZhuUnit:) name:TDCeoMeZhuUnit object:nil];
    
    //主售房源单元
    
    if (_oneArr.count == 0)
    {
        //TODO:（主售）单元
        [_manager userCEOZhuUnitWithID:_proID];
        
        
    }
    
   // ceo主售房源信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoMeZhuHouse:) name:TDCeoMeZhuHouse object:nil];
    
    
    
    
    
    
    //同意或者驳回的请求
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoMeZhuDJSP:) name:TDCeoMeZhuDJSP object:nil];
    
    
}

//表头通知方法
-(void)didTDCeoMeZhuUnit:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _oneArr = [notifi.object objectForKey:@"data"];
            
            
            _zhuUnitDic = [_oneArr objectAtIndex:0];
            
            _unitStr = [_zhuUnitDic objectForKey:@"name"];
            // NSLog(@"======11111====_louStr %@ ",_unitStr);
            for (int i = 0; i < _oneArr.count; i++)
            {
                
                NSDictionary *dict = [_oneArr objectAtIndex:i];
                [_nameArr addObject:dict[@"name"]];
                
                NSLog(@"000000=====%@",_nameArr);
            }
            
            [_tabView reloadData];
            [self setupMenuQiArr:_nameArr];
            [super viewDidLoad];
            
            [_manager userCEOZhuhousewithID:_proID withUnit:_unitStr];

           
        }
        
    }
        else
        {
            [_manager MakeMyselfAlert:@"获取失败" andIsNeedTitle:NO andShowTime:2.0];
   
        }
  
}
    
  //房源的通知方法
-(void)didTDCeoMeZhuHouse:(NSNotification *)noti
{
    if (noti.object != nil)
    {
        
        _hosZArr = [noti.object objectForKey:@"data"];
     // NSLog(@"000===这个数组里有什么======%@",_hosZArr);
        for (int i = 0; i<_hosZArr.count; i++)
        {
            NSMutableDictionary *temDic = _hosZArr[i];
            NSArray *temArr = [temDic objectForKey:@"data"];
            [_room_numArr addObject:temArr];
            //NSLog(@"==================%@",_room_numArr);
        }
        
        
        
             [_tabView reloadData];
    }
    
}

//驳回或者同意
-(void)didTDCeoMeZhuDJSP:(NSNotification *)notifi
{
    
    if (notifi.object != nil)
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
    
    
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoMeZhuUnit object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoMeZhuHouse object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoMeZhuHouse object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"价格表";
    self.view.backgroundColor = BgColor;
    
    _oneArr = [NSMutableArray array];
    _nameArr = [NSMutableArray array];
    _zhuUnitDic = [NSMutableDictionary dictionary];
    //房源
    _hosMDic = [NSMutableDictionary dictionary];
    _hosZArr = [NSMutableArray array];
    _floorArr = [NSMutableArray array];
    _hangArr = [NSMutableArray array];
    _wenArr = [NSMutableArray array];
    _erDateArr = [NSMutableArray array];
    _room_numArr = [NSMutableArray array];
    
    
    
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+44, B_WIDTH, B_HEIGHT-110) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 44;
    _tabView.backgroundColor = BgColor;
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
   

    [self.view addSubview:_tabView];
    //导航右侧的定价说明按钮
    
    UIButton * button_back = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 20.0f)];
    [button_back setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
    [button_back.layer setCornerRadius:4.0f];
    [button_back.layer setBorderColor:[UIColor whiteColor].CGColor];
    [button_back.layer setBorderWidth:1.0f];
    button_back.titleLabel.font = [UIFont systemFontOfSize:12];
    [button_back setTitleColor:[UIColor whiteColor] forState:0];
    [button_back setTitle:@"定价说明" forState:UIControlStateNormal];
    [button_back.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [button_back addTarget:self action:@selector(unitDingJiaBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button_back];
    [backButton setStyle:UIBarButtonItemStyleDone];
    [self.navigationItem setRightBarButtonItem:backButton];
    

    _manager = [TodoHttpManager sharedManager];

    [self setupMenuQiArr:_nameArr];
    
    NSLog(@"0--------%@",_proID);
}


-(void)setupMenuQiArr:(NSMutableArray *)nameArr
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    RKTagsMenuView *tagMenuView = [[RKTagsMenuView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight,self.view.frame.size.width, 44)];
    tagMenuView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tagMenuView];
    [tagMenuView setTagsArray:nameArr];
    //[_manager userCEOZhuhousewithID:_proID withUnit:_unitStr];
    [_tabView reloadData];

    [tagMenuView setDidSelectTagBlock:^(NSInteger clickedRow) {
        
      
        if (_hosZArr.count != 0)
        {
            
  
            [_room_numArr removeAllObjects];
            
           // [_hangArr removeAllObjects];
            
              [_manager userCEOZhuhousewithID:_proID withUnit:nameArr[clickedRow]];
        }
     
    
        
        
    }];
    
    
}

-(void)unitDingJiaBtnClick
{
    CeoZhuDingVC *dingVc = [[CeoZhuDingVC alloc] init];
    dingVc.pID = _proID;
    NSLog(@"-------------%@",_proID);
    [self.navigationController pushViewController:dingVc animated:NO];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _hosZArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellId = @"Cell";
    CeoZhuXCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[CeoZhuXCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    if (_hosZArr.count != 0)
    {
        _hangArr =  [[_hosZArr objectAtIndex:indexPath.row] objectForKey:@"data"];
        NSLog(@"================%ld",_hangArr.count);
 
        cell.oneLab.text =[[_hosZArr objectAtIndex:indexPath.row] objectForKey:@"floor"];
  
    }

    if ([cell isKindOfClass:[CeoZhuXCell class]])
    {
        if (_hosZArr.count != 0)
        {
           // NSDictionary *dict = [_dataArr objectAtIndex:indexPath.row];
            
            [(CeoZhuXCell *)cell setupCustomTableViewCellWIthBtnArr:_hangArr withShangLab:_room_numArr[indexPath.row] withXiaLab:nil];
            
    
        }
    }

    
  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
    cell.backgroundColor = BgColor;
   
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger number = 1;
     if (_hosZArr.count != 0)
     {
         if (_hangArr.count %3 == 0)
         {
             number =( _hangArr.count)/3;
         }
         else
         {
             if (_hangArr.count /3 < 1)
             {
                 number = 1;
             }
             else
             {
                 number = _hangArr.count/3 + 1;
             }
         }
 
     }
     

    return number *42.f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 0.5)];
    linLab.backgroundColor = [UIColor lightGrayColor];
    [footView addSubview:linLab];
    UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //agreeBtn.frame = CGRectMake(20, 5, 80, 30);
    agreeBtn.layer.cornerRadius = 5;
    agreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [agreeBtn setTitle:@"同意执行" forState:0];
    [agreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [agreeBtn setBackgroundColor:[UIColor orangeColor]];
    [agreeBtn addTarget:self action:@selector(agreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:agreeBtn];
    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.right.equalTo(footView.mas_centerX).offset(-10);
         make.top.equalTo(footView.mas_top).offset(5);
    }];
    
    UIButton *unAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    unAgreeBtn.layer.cornerRadius = 5;
    unAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [unAgreeBtn setTitle:@"驳回修改" forState:0];
    [unAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [unAgreeBtn setBackgroundColor:[UIColor orangeColor]];
     [unAgreeBtn addTarget:self action:@selector(unAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:unAgreeBtn];
    [unAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.left.equalTo(footView.mas_centerX).offset(10);
        make.top.equalTo(footView.mas_top).offset(5);
    }];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([_typeStr  isEqualToString:@"待审批"])
    {
        return 40;
    }
    else
    {
      return 0;
    }
    
}


-(void)agreeBtnClick:(UIButton *)btn
{
    
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"同意执行?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            
            [_manager userCEOZhuDJShenPiWithID:_proID withUserId:IDA withType:@"2" withXiuOpinion:nil];
            
            [self.navigationController popViewControllerAnimated:NO];
        }
      
  
       
    }];
                              [agreeView showLXAlertView];
                              

    
   

}
-(void)unAgreeBtn:(UIButton *)btn
{
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"驳回修改?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            
            [_manager userCEOZhuDJShenPiWithID:_proID withUserId:IDA withType:@"3" withXiuOpinion:nil];
            
            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
    
 
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
